package com.tbit.uqbike.client.handler.borrowbikehandler;

import com.tbit.common.utils.TraceUtil;
import com.tbit.uqbike.client.common.ResultMsg;
import com.tbit.uqbike.client.dao.core.BorrowCheckConfigLogDao;
import com.tbit.uqbike.client.dao.core.LeaderAccountDao;
import com.tbit.uqbike.client.pojo.*;
import com.tbit.uqbike.client.pojo.borrowbike.BorrowCheckConfigLog;
import com.tbit.uqbike.client.pojo.newEntity.LeaderAccount;
import com.tbit.uqbike.client.service.AccountConfigService;
import com.tbit.uqbike.client.service.AdAccountFeeService;
import com.tbit.uqbike.client.service.GiftCardUserService;
import com.tbit.uqbike.client.service.RideLogService;
import com.tbit.uqbike.client.vo.borrow.BorrowBikeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author lh2
 * @ClassName: RechargeCheckHandler
 * @Description: 充值金额和骑行最低金额校验处理器
 * @date 2023-07-27 11:30:06
 */
@Slf4j
@Component
public class RechargeCheckHandler extends AbstractCheckHandler {
    @Autowired
    private AccountConfigService accountConfigService;
    @Autowired
    private LeaderAccountDao leaderAccountDao;
    @Autowired
    private GiftCardUserService giftCardUserService;
    @Autowired
    private AdAccountFeeService adAccountFeeService;
    @Autowired
    private RideLogService rideLogService;
    @Autowired
    private BorrowCheckConfigLogDao borrowCheckConfigLogMapper;

    @Override
    public ResultMsg handle(BorrowBikeVO vo, String serialNO) {
        log.info("【充值金额和骑行最低金额校验处理器】校验开始...");
        // 降级：如果配置了降级，则跳过此处理器，执行下一个处理器
        if (super.getConfig().getDown() == 1) {
            log.info("【充值金额和骑行最低金额校验处理器】被降级，已自动跳过！");
            return super.next(vo, serialNO);
        }

        User user = vo.getUser();
        Machine machine = vo.getMachine();
        AccountConfig accountConfig = accountConfigService.getByAccountId(machine.getAccountId());
        Integer rideNum = 0;
        List<RideLog> rideLogs = rideLogService.getByUserId(user.getUserId());
        if (rideLogs.size() > 0) {
            rideNum = rideLogs.size();
        }
        /** 兼容领导账号不需要购买 */
        LeaderAccount leaderAccount = leaderAccountDao.getByPhone(user.getPhone(), user.getAccountId());
        if (leaderAccount == null || System.currentTimeMillis() >= leaderAccount.getExpireTime().getTime()) {
            AdAccountFee adAccountFee = adAccountFeeService.getByAccountId(machine.getAccountId(),
                    machine.getMachineId());
            if (adAccountFee == null) {
                return ResultMsg.fail(-3056, "计费规则未配置", TraceUtil.getTraceIdByLocal());
            }
            /** 判断是否要求预充值 */
            if (adAccountFee != null && adAccountFee.getNewUserRechcrge() > 0) {
                Integer rideLogCount = rideNum;
                // Integer rideLogCount=userSta.getRideTimes();
                Double needRechargeMoney = adAccountFee.getNewUserRechcrge() - user.getMoney();
                if (rideLogCount == 0 && needRechargeMoney > 0) {
                    return ResultMsg.fail(-3049, "请先充值再骑行",TraceUtil.getTraceIdByLocal());
                }
            }
            Integer giftCardMoney = 0;
            /** 礼品卡余额 */
            GiftCardUser giftCardUser = giftCardUserService.getByUserId(user.getUserId(), machine.getAccountId());
            if (giftCardUser != null) {
                giftCardMoney = giftCardUser.getMoney();
            }
            /** 兼容支付分订单 */
            if (adAccountFee.getRechargeBase() > 0 && (giftCardMoney + user.getMoney()) < adAccountFee.getRechargeBase()
                    && accountConfig != null && accountConfig.getWxZffMm() == 0) {
                return ResultMsg.fail(-3055, "存在最低账户余额限制",TraceUtil.getTraceIdByLocal());
            }
        }

        log.info("【充值金额和骑行最低金额校验处理器】校验通过！");
        BorrowCheckConfigLog bccLog = borrowCheckConfigLogMapper.findByNameAndSerialNO(
                this.getConfig().getHandlerName(), serialNO);
        if (bccLog != null) {
            bccLog.setExecuteResult(1);
            borrowCheckConfigLogMapper.updateByPrimaryKeySelective(bccLog);
        }

        // 执行下一个处理器
        return super.next(vo, serialNO);
    }

}
